package blog.open1111.ceawler;

import blog.open1111.util.DateUtil;
import blog.open1111.util.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import org.jsoup.helper.DataUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * Created by dengwubo on 2017/9/7.
 */
public class DownLoadImageTest {

    private static Logger logger = Logger.getLogger(DownLoadImageTest.class);
    private static final String link = "http://images2015.cnblogs.com/blog/952033/201705/952033-20170511210141910-342481715.png";


    public static void main(String[] args)  {
        logger.info("开始爬取"+link+"图片");

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(link); // 创建httpGet实例
        RequestConfig config = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(5000).build();
        httpGet.setConfig(config);

        CloseableHttpResponse response = null;

        try {
            response=httpClient.execute(httpGet);
        } catch (ClientProtocolException e) {
            logger.error("ClientProtocolException",e);
        } catch (IOException e) {
            logger.error("IOException",e);
        }


        if (response!=null){
            HttpEntity entity = response.getEntity();
            if (response.getStatusLine().getStatusCode()==200){
                try {

                    InputStream inputStream = entity.getContent();
                    String imageType = entity.getContentType().getValue();
                    System.out.println("imageType:"+imageType);
                    String urlB = imageType.split("/")[1];
                    String uuid = UUID.randomUUID().toString();
                    FileUtils.copyToFile(inputStream,new File(PropertiesUtil.getValue("imageFilePath")+ DateUtil.getCurrentDatePath()+"/"+uuid+"."+urlB));

                } catch (UnsupportedOperationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                logger.error("返回状态非200");
            }
        }else{
            logger.error("连接超时");
        }
        try{

            if (response!=null)response.close();
            if (httpClient!=null) httpClient.close();
        }catch (Exception e){
            logger.error("Exception",e);
        }
        logger.info("结束爬取"+link+"图片");


    }



}
